package com.collect.collectclient.xml.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import com.collect.collectclient.xml.dto.request.SendMessageRequest;
import com.collect.collectclient.xml.dto.response.*;
import com.collect.collectclient.xml.mapper.XmlRepository;
import com.collect.collectclient.xml.service.SendMessageService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Service
public class SendMessageServiceImpl implements SendMessageService {
    @Resource
    XmlRepository xmlRepository;
    @Override
    public Message getMessageBySendMessage(SendMessageRequest request) throws UnknownHostException {

        if (ObjectUtils.isEmpty(request.getCount()))
        {
            request.setCount(1);
        }
        Address address=Address.builder().sys(request.getSys())
                .build();
        From from=From.builder().address(address).build();
        SysInfo sysInfo=SysInfo.builder().sysVersion("1.0").sysName("交通管理系统")
                    .sysType("交通诱导可变标志").supplier("模拟供应商").regionIDList(Lists.newArrayList("1")).build();
        if (!ObjectUtils.isEmpty(request.getSysName()))
        {
            sysInfo.setSysName(request.getSysName());
        }
        sysInfo.setSysType(ObjectUtils.isEmpty(request.getSysType())?"交通诱导可变标志": request.getSysType());
        sysInfo.setSysVersion(ObjectUtils.isEmpty(request.getSysVersion())?"1.0": request.getSysVersion());
        sysInfo.setSupplier(ObjectUtils.isEmpty(request.getSupplier())?"模拟供应商": request.getSupplier());
       if( CollUtil.isNotEmpty(request.getRegionIDList())){
           sysInfo.setRegionIDList(request.getRegionIDList());
       }

        TrafficAlarm trafficAlarm=xmlRepository.getAlarmList();
//                TrafficAlarm.builder()
//                .alarmType("事故类型").programID("节目编号-01")
//                .affect("影响范围").content("事故简介").fileUrl("http://wwww.baidu.com/xml")
//                .latitude(88.256).longitude(118.56).location("Roy Street+9th Avenue North")
//                .occurTime("2024-06-09T21:03:09").build();
//        RoadTrafficStatus roadTrafficStatus=RoadTrafficStatus.builder()
//                .roadStatus("1")
//                .speed("120").detectTime("2024-06-09T21:03:09").locationID("101000001")
//                .locationName("Roy Street+9th Avenue North").direction("东直右")
//                .fileUrl("http://wwww.baidu.com/xml").programID("100231")
//        .build();
        RoadTrafficStatus roadTrafficStatus=xmlRepository.getRoadTrafficList();
        TrafficManagement trafficManagement=xmlRepository.getManagementList();
//                TrafficManagement.builder().ManagementType("区域管制")
//                .programID("00452").reason("交通拥堵").startTime("2024-05-20 20:00:00")
//                .fileUrl("http://www.baidu.com").content("由于交通拥堵，1路口实行交通管制")
//                .endTime("2024-05-20 20:30:00").build();

        TrafficEvent trafficEvent=xmlRepository.getEventList();
//                TrafficEvent.builder()
//                .eventType("追尾")
//                .programID("45990--02").affect("1路口-1道路全体")
//                .content("由于追尾导致1路口限行").occurTime("2024-05-20 20:13:45")
//                .fileUrl("http://www.baidu.com").latitude(20.4514).longitude(123.65472)
//                .location("Roy Street+9th")
//                .build();

        OtherInfo info=new OtherInfo();
        info.setProgramID(UUID.randomUUID().toString().replace("-",""));
        info.setFileUrl("http://xxx");
        if (!ObjectUtils.isEmpty(request.getOtherContent()))
        {
            info.setContent(request.getOtherContent());
        }
        else {
            info.setContent("默认数据");
        }
//                OtherInfo.builder().content("Roy Street+9th").programID("908890222")
//                .fileUrl("http://www.baidu.com").build();

        ContentList contentList=ContentList.builder()
                .roadTrafficStatus(roadTrafficStatus)
                .trafficAlarm(trafficAlarm)
                .trafficManagement(trafficManagement)
                .trafficEvent(trafficEvent)
                .otherInfo(info)
                .build();
        OtherSendPlayScheme otherSendPlayScheme=OtherSendPlayScheme.builder().schemeID("播放方案01")
                .type("1").contentList(contentList).build();


        Operation operation=Operation.builder().sysInfo(sysInfo).otherSendPlayScheme(otherSendPlayScheme)
                .build();
        Body body= Body.builder().operation(operation).build();
        Message message=Message.builder().Body(body).From(from)

                .To(InetAddress.getLocalHost().getHostAddress()).version("1.0").Token("AilyOR3SKe5cAApwPb5ykBTA6WA").build();
        return message;
    }
}
